import {fetchVodListReq} from "../apis/goApis";
import {handleResp} from "../utils/commonfunc";

export const vod = {
    state: {
        vodList: [],
    },

    reducers: {
        setVodList(state, payload) {

            const {
                p,
                list,
            } = payload
            // return {
            //     ...state,
            //     vodList: [
            //         ...state.vodList,
            //         ...list,
            //     ],
            // }
            if (p !== 0) {
                return {
                    ...state,
                    vodList: [
                        ...state.vodList,
                        ...list,
                    ],
                }
            } else {
                return {
                    ...state,
                    vodList: [
                        ...list,
                    ],
                }
            }

        },
    },

    effects: (dispatch) => ({

        async doFetchVodList(payload, rootState) {
            const resp = await fetchVodListReq(payload)

            handleResp(resp, dispatch, (data) => {
                const list = data.ok.data
                if (list.length === 0) {
                    if (payload.endCallback) {
                        payload.endCallback()
                    }
                }
                dispatch.vod.setVodList({p: payload.p, list,})
            })
        },
    })
}
